package com.vanguard.service.impl;

import com.vanguard.commons.echarts.CategoryAxis;
import com.vanguard.commons.echarts.OptionData;
import com.vanguard.commons.echarts.PieData;
import com.vanguard.commons.exception.BusinessException;
import com.vanguard.mapper.ProductMapper;
import com.vanguard.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Title: 商品业务层
 * @Description:
 * @Author: vanguard
 * @Version: 1.0
 * @Date: 2019/03/12
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public OptionData selectProductType() throws BusinessException {
        List<Map<String, Object>> list = productMapper.selectProductType();
        OptionData option = new OptionData();
        //定义存放数据的集合
        List data = new ArrayList();
        //循环数据
        for(Map<String, Object> objectMap : list) {
            //饼状数据
            data.add(new PieData(objectMap.get("CNAME").toString(), objectMap.get("TOTAL").toString()));
        }
        option.setOptionData(data);
        //设置数据
        return option;
    }

    @Override
    public OptionData selectProductTypeBar() {
        List<Map<String, Object>> list = productMapper.selectProductType();
        OptionData option = new OptionData();
        List data = new ArrayList();
        CategoryAxis categoryAxis = new CategoryAxis();
        //循环数据
        for(Map<String, Object> objectMap : list) {
            //柱状图数据
           data.add(objectMap.get("TOTAL"));
           //柱状图类目数据
           categoryAxis.data((objectMap.get("CNAME")));
        }
        option.xAxis(categoryAxis);
        option.setOptionData(data);
        return option;
    }
}
